package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.come.bean.PalacePkBean;
import org.come.mouslisten.PkPutLabMouseListener;
import org.come.until.CutButtonImage;
import org.come.until.LimitNumTextArea;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.WorkshopBtn;
import com.tool.tcpimg.UIUtils;

 

public class PalacePKJpanel extends JPanel {

    private JTextField textName,// 姓名
            textGold;// 金钱
    private boolean[] putChooseType; // 投入三个选择框的选中标识
    private int putTextType; // 当前输入框是哪个类型的 0是金钱 1是仙玉 2是经验
    private JLabel[] putLab, // 投入三个选择框
            putFundLab; // 投入资金三个信息栏
    private JLabel remarkLab; // 展示名称以及胜利信息
    private JLabel putIntoLab, // 投入勾选框
            challengeLab, // 下战书勾选框
            sendBellLab; // 发送铃铛勾选框
    private boolean putIntoBool = true, // 投入勾标识
            challengeBool = true, // 下战书勾标识
            sendBellBool = true; // 发送铃铛标识
    private WorkshopBtn workshopBtn, // 下战书按钮
            cancelBtn; // 取消按钮
    private JLabel putUnit; // 单位
    private String[] unitString = { "两", "仙玉", "经验", "消耗金钱：", "消耗仙玉：", "消耗经验：" };
    private String[] fundString; // 存储金额
    private LimitNumTextArea sendBelTextArea; // 铃铛发送输入框;

    private int winnerType = 0;// 擂主标识 0皇宫PK 1擂主赛 2接收皇宫PK 3接收擂主赛
    private PalacePkBean palacePkBean;

    public PalacePKJpanel() {

        this.setPreferredSize(new Dimension(442, 385));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);
        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 66);
        offBtn.setBounds(442 - 37, 10, 25, 25);
        this.add(offBtn);
        getPutLab();
        this.add(getCancelBtn());
        this.add(getPutUnit());
        this.add(getSendBelTextArea());
        getPutChooseType();
        getFundString();
        getRemarkLab();
        // 姓名
        textName = new JTextField();
        textName.setBounds(104 + 67 + 12 + 22, 50 - 10 - 11, 98, 16);
        textName.setCaretColor(Color.white);
        textName.setOpaque(false);
        textName.setBorder(BorderFactory.createEmptyBorder());
        textName.setForeground(Color.white);
        textName.setFont(UIUtils.TEXT_FONT1);
        this.add(textName);

        // 金钱
        textGold = new JTextField();
        textGold.setBounds(87, 112, 101, 16);
        textGold.setCaretColor(Color.white);
        textGold.setOpaque(false);
        textGold.setBorder(BorderFactory.createEmptyBorder());
        textGold.setForeground(Color.white);
        textGold.setFont(UIUtils.TEXT_FONT1);
        textGold.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                if (putChooseType[putTextType]) {
                    fundString[putTextType] = "".equals(textGold.getText()) == true ? "0" : textGold.getText();
                    putFundLab[putTextType].setText(unitString[putTextType + 3] + (fundString[putTextType])
                            + unitString[putTextType]);
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                if (putChooseType[putTextType]) {
                    fundString[putTextType] = ("".equals(textGold.getText()) == true ? "0" : textGold.getText());
                    putFundLab[putTextType].setText(unitString[putTextType + 3] + (fundString[putTextType])
                            + unitString[putTextType]);
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        textGold.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

                int charstr = e.getKeyChar();
                // control the value between 0 and 9
                if (charstr < KeyEvent.VK_0 || charstr > KeyEvent.VK_9) {
                    e.consume();
                    return;
                }
                String str = textGold.getText();
                if (str.length() == 0) {
                    textGold.setText("");
                }
                if (str.length() == 1) {
                    if (Long.parseLong(str) == 0) {
                        textGold.setText("");
                    }
                }

            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
        this.add(textGold);

        this.add(getPutIntoLab());
        this.add(getChallengeLab());
        this.add(getSendBellLab());
        getPutFundLab();
        // 下战书按钮
        workshopBtn = new WorkshopBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "确定", 12,
                this);
        workshopBtn.setBounds(101, 346, 99, 24);
        this.add(workshopBtn);

        changeView(0);
    }

    private ImageIcon icon = new ImageIcon("inkImg/old/background/S119.png");
    private ImageIcon iconLeft = CutButtonImage.getImage("inkImg/old/background/S120.png", -1, -1);
    private ImageIcon pkNameImg;

    // private ImageIcon challengeImg;// 花费20,000,000银两）_w127,h12.png

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        g.drawImage(icon.getImage(), 0, 0, 442, 385, this);
        g.drawImage(iconLeft.getImage(), 0, 0, 43, 339, this);
        if (winnerType == 0) {
            if (pkNameImg == null) {
                pkNameImg = CutButtonImage.getImage("inkImg/old/background/S122.png", 132, 19);
            }
            g.drawImage(pkNameImg.getImage(), 104 + 67 + 10 + 22, 50 - 12 - 11, 132, 19, this);
        }

        // if (challengeImg == null) {
        // challengeImg =
        // CutButtonImage.getImage("inkImg/hongmu/2/花费5,000,000银两）_w120,h12.png",
        // 127, 12);
        // }
        // g.drawImage(challengeImg.getImage(), 220, 175, 127, 12, this);
    }

    // 切换面板展示
    public void changeView(int type) {
        winnerType = type;
        clearViewData();
        if (winnerType == 0) {
            changeViewPalaceReception(false);
            changeViewWinnerReception(false);
            changeViewWinner(false);
            changeViewPalace(true);
        } else if (winnerType == 1) {
            changeViewPalace(false);
            changeViewPalaceReception(false);
            changeViewWinnerReception(false);
            changeViewWinner(true);
        } else if (winnerType == 2) {
            changeViewPalace(false);
            changeViewWinner(false);
            changeViewWinnerReception(false);
            changeViewPalaceReception(true);
        } else if (winnerType == 3) {
            changeViewPalace(false);
            changeViewWinner(false);
            changeViewPalaceReception(false);
            changeViewWinnerReception(true);

        }
    }

    // 切换到皇宫PK界面展示
    public void changeViewPalace(boolean type) {
        if (type) {
            iconLeft = CutButtonImage.getImage("inkImg/old/background/S120.png", -1, -1);
            remarkLab.setText("输入决斗对象名字:");
            workshopBtn.setText("确认");
            cancelBtn.setText("取消");
            for (int i = 0; i < putFundLab.length; i++) {
                putFundLab[i].setText(unitString[i + 3] + fundString[i] + unitString[i]);
            }
        }
        textName.setVisible(type);
        textGold.setEditable(type);
    }

    // 切换到皇宫PK接收界面展示
    public void changeViewPalaceReception(boolean type) {
        if (type) {
            iconLeft = CutButtonImage.getImage("inkImg/old/background/S120.png", -1, -1);
            workshopBtn.setText("接受挑战");
            cancelBtn.setText("认怂投降");
            putFundLab[0].setText("下注金钱：" + fundString[0] + "金钱");
            putFundLab[1].setText("下注仙玉：" + fundString[1] + "仙玉");
            putFundLab[2].setText("下注经验：" + fundString[2] + "经验");
            putIntoLab.setIcon(null);
            challengeLab.setIcon(null);
            sendBellLab.setIcon(null);
            putIntoBool = !type;
            challengeBool = !type;
            sendBellBool = !type;
        }
        textGold.setEditable(!type);
        textName.setVisible(!type);
    }

    // 切换到擂主界面展示
    public void changeViewWinner(boolean type) {
        if (type) {
            iconLeft = CutButtonImage.getImage("inkImg/old/background/S121.png", -1, -1);
            remarkLab.setText("挑战成功者升为擂主");
            workshopBtn.setText("确认");
            cancelBtn.setText("取消");
            for (int i = 0; i < putFundLab.length; i++) {
                putFundLab[i].setText(unitString[i + 3] + fundString[i] + unitString[i]);
            }
        }
        textName.setVisible(!type);
        textGold.setEditable(type);
    }

    // 切换到接收擂主战书面板
    public void changeViewWinnerReception(boolean type) {
        if (type) {
            iconLeft = CutButtonImage.getImage("inkImg/old/background/S121.png", -1, -1);
            remarkLab.setText("挑战成功者升为擂主");
            workshopBtn.setText("接受挑战");
            cancelBtn.setText("认怂投降");
            putFundLab[0].setText("下注金钱：" + fundString[0] + "金钱");
            putFundLab[1].setText("下注仙玉：" + fundString[1] + "仙玉");
            putFundLab[2].setText("下注经验：" + fundString[2] + "经验");
            putIntoLab.setIcon(null);
            challengeLab.setIcon(null);
            sendBellLab.setIcon(null);
            putIntoBool = !type;
            challengeBool = !type;
            sendBellBool = !type;
        }
        textName.setVisible(!type);
        textGold.setEditable(!type);
    }

    // 清空数据
    public void clearViewData() {
        for (int i = 0; i < fundString.length; i++) {
            fundString[i] = "0";
        }
        sendBelTextArea.setText("");
        textGold.setText("");
        textName.setText("");
        for (int i = 0; i < putLab.length; i++) {
            putLab[i].setIcon(null);
            putChooseType[i] = false;
        }
    }

    /**
     * 接收到战书后展示面板
     * 
     * @param palacePkBean
     */
    public void changeViewReception(PalacePkBean palacePkBean) {
        this.palacePkBean = palacePkBean;
        changeView(2);
        String username = palacePkBean.getUsername();
        BigDecimal gold = palacePkBean.getGold();
        BigDecimal xianyu = palacePkBean.getXianyu();
        BigDecimal exp = palacePkBean.getExp();
        if (gold == null) {
            gold = new BigDecimal(0);
        }
        if (xianyu == null) {
            xianyu = new BigDecimal(0);
        }
        if (exp == null) {
            exp = new BigDecimal(0);
        }

        remarkLab.setText("【" + username + "】玩家向您下达了战书");
        putFundLab[0].setText("下注金钱:" + gold + "两");
        fundString[0] = gold.toString();
        if (gold.compareTo(new BigDecimal(0)) > 0) {
            putLab[0].setIcon(CutButtonImage.getImage("skin/8100226/button/B118.png", 7, 7));
            putChooseType[0] = true;
        } else {
            putLab[0].setIcon(null);
            putChooseType[0] = false;
        }
        putFundLab[1].setText("下注仙玉:" + xianyu + "仙玉");
        fundString[1] = xianyu.toString();
        if (xianyu.compareTo(new BigDecimal(0)) > 0) {
            putLab[1].setIcon(CutButtonImage.getImage("skin/8100226/button/B118.png", 7, 7));
            putChooseType[1] = true;
        } else {
            putLab[1].setIcon(null);
            putChooseType[2] = false;
        }
        putFundLab[2].setText("下注经验:" + exp + "经验");
        fundString[2] = exp.toString();
        if (exp.compareTo(new BigDecimal(0)) > 0) {
            putLab[2].setIcon(CutButtonImage.getImage("skin/8100226/button/B118.png", 7, 7));
            putChooseType[1] = true;
        } else {
            putLab[2].setIcon(null);
            putChooseType[2] = false;
        }
        for (int i = 0; i < putChooseType.length; i++) {
            if (putChooseType[i]) {
                putIntoBool = true;
                putIntoLab.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", 15, 15));
            }
        }
    }

    // 接收到擂主赛展示面板
    public void changeViewWinnerReception(PalacePkBean palacePkBean) {
        this.palacePkBean = palacePkBean;
        changeView(3);
        String username = palacePkBean.getUsername();
        BigDecimal gold = palacePkBean.getGold();
        BigDecimal xianyu = palacePkBean.getXianyu();
        BigDecimal exp = palacePkBean.getExp();
        if (gold == null) {
            gold = new BigDecimal(0);
        }
        if (xianyu == null) {
            xianyu = new BigDecimal(0);
        }
        if (exp == null) {
            exp = new BigDecimal(0);
        }

        putFundLab[0].setText("下注金钱:" + gold + "两");
        fundString[0] = gold.toString();
        if (gold.compareTo(new BigDecimal(0)) > 0) {
            putLab[0].setIcon(CutButtonImage.getImage("skin/8100226/button/B118.png", 7, 7));
            putChooseType[0] = true;
        } else {
            putLab[0].setIcon(null);
            putChooseType[0] = false;
        }
        putFundLab[1].setText("下注仙玉:" + xianyu + "仙玉");
        fundString[1] = xianyu.toString();
        if (xianyu.compareTo(new BigDecimal(0)) > 0) {
            putLab[1].setIcon(CutButtonImage.getImage("skin/8100226/button/B118.png", 7, 7));
            putChooseType[1] = true;
        } else {
            putLab[1].setIcon(null);
            putChooseType[2] = false;
        }
        putFundLab[2].setText("下注经验:" + exp + "经验");
        fundString[2] = exp.toString();
        if (exp.compareTo(new BigDecimal(0)) > 0) {
            putLab[2].setIcon(CutButtonImage.getImage("skin/8100226/button/B118.png", 7, 7));
            putChooseType[1] = true;
        } else {
            putLab[2].setIcon(null);
            putChooseType[2] = false;
        }
        for (int i = 0; i < putChooseType.length; i++) {
            if (putChooseType[i]) {
                putIntoBool = true;
                putIntoLab.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", 15, 15));
            }
        }

    }

    public JTextField getTextName() {
        return textName;
    }

    public void setTextName(JTextField textName) {
        this.textName = textName;
    }

    public JTextField getTextGold() {
        return textGold;
    }

    public void setTextGold(JTextField textGold) {
        this.textGold = textGold;
    }

    public WorkshopBtn getWorkshopBtn() {
        return workshopBtn;
    }

    public void setWorkshopBtn(WorkshopBtn workshopBtn) {
        this.workshopBtn = workshopBtn;
    }

    public JLabel[] getPutLab() {
        if (putLab == null) {
            putLab = new JLabel[3];
            for (int i = 0; i < putLab.length; i++) {
                putLab[i] = new JLabel("", SwingConstants.CENTER);
                putLab[i].setBounds(86 + i % 3 * 81 + (i == 1 ? -1 : 0), 88, 17, 17);
                putLab[i].addMouseListener(new PkPutLabMouseListener(i, this));
                this.add(putLab[i]);
            }
        }
        return putLab;
    }

    public void setPutLab(JLabel[] putLab) {
        this.putLab = putLab;
    }

    public WorkshopBtn getCancelBtn() {
        if (cancelBtn == null) {
            cancelBtn = new WorkshopBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "取消",
                    14, this);
            cancelBtn.setBounds(241, 346, 99, 24);
        }
        return cancelBtn;
    }

    public void setCancelBtn(WorkshopBtn cancelBtn) {
        this.cancelBtn = cancelBtn;
    }

    public JLabel getPutIntoLab() {
        if (putIntoLab == null) {
            putIntoLab = new JLabel();
            putIntoLab.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", 15, 15));
            putIntoLab.setBounds(68, 66, 15, 15);
            putIntoLab.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    if (winnerType == 2 || winnerType == 3) {
                        return;
                    }

                    if (putIntoBool) {
                        putIntoLab.setIcon(null);
                        for (int i = 0; i < putLab.length; i++) {
                            putLab[i].setIcon(null);
                            putChooseType[i] = false;
                            fundString[i] = "0";
                            putFundLab[i].setText(unitString[i + 3] + (fundString[i]) + unitString[i]);
                        }
                        textGold.setText("");
                        textGold.setEditable(false);
                    } else {
                        putIntoLab.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", 15, 15));
                        textGold.setEditable(true);
                    }
                    putIntoBool = !putIntoBool;

                }
            });
        }
        return putIntoLab;
    }

    public void setPutIntoLab(JLabel putIntoLab) {
        this.putIntoLab = putIntoLab;
    }

    public JLabel getChallengeLab() {
        if (challengeLab == null) {
            challengeLab = new JLabel();
            challengeLab.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", 15, 15));
            challengeLab.setBounds(68, 159, 15, 15);
            challengeLab.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    // TODO Auto-generated method stub
                    if (challengeBool) {
                        challengeLab.setIcon(null);

                    } else {
                        challengeLab.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", 15, 15));

                    }
                    challengeBool = !challengeBool;
                }
            });
        }
        return challengeLab;
    }

    public void setChallengeLab(JLabel challengeLab) {
        this.challengeLab = challengeLab;
    }

    public JLabel getSendBellLab() {
        if (sendBellLab == null) {
            sendBellLab = new JLabel();
            sendBellLab.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", 15, 15));
            sendBellLab.setBounds(68, 182, 15, 15);
            sendBellLab.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    // TODO Auto-generated method stub
                    if (sendBellBool) {
                        sendBellLab.setIcon(null);
                    } else {
                        sendBellLab.setIcon(CutButtonImage.getImage("skin/8100226/button/B88.png", 15, 15));
                    }
                    sendBellBool = !sendBellBool;
                }
            });
        }
        return sendBellLab;
    }

    public void setSendBellLab(JLabel sendBellLab) {
        this.sendBellLab = sendBellLab;
    }

    public JLabel[] getPutFundLab() {
        if (putFundLab == null) {
            putFundLab = new JLabel[3];
            for (int i = 0; i < putFundLab.length; i++) {
                putFundLab[i] = new JLabel();
                putFundLab[i].setBounds(207 + 25, 120 - 12 + i % 3 * 17, 175, 17);
                // putFundLab[i].setOpaque(true);
                putFundLab[i].setFont(UIUtils.TEXT_FONT);
                putFundLab[i].setForeground(Color.red);
                this.add(putFundLab[i]);
                if (i == 0) {
                    putFundLab[i].setText("消耗金钱：0两");
                }
                if (i == 1) {
                    putFundLab[i].setText("消耗仙玉：0仙玉");
                }
                if (i == 2) {
                    putFundLab[i].setText("消耗经验：0经验");
                }
            }
        }
        return putFundLab;
    }

    public void setPutFundLab(JLabel[] putFundLab) {
        this.putFundLab = putFundLab;
    }

    public boolean[] getPutChooseType() {
        if (putChooseType == null) {
            putChooseType = new boolean[3];

        }
        return putChooseType;
    }

    public void setPutChooseType(boolean[] putChooseType) {
        this.putChooseType = putChooseType;
    }

    public int getPutTextType() {
        return putTextType;
    }

    public void setPutTextType(int putTextType) {
        this.putTextType = putTextType;
    }

    public JLabel getPutUnit() {
        if (putUnit == null) {
            putUnit = new JLabel();
            putUnit.setOpaque(false);
            putUnit.setBounds(191, 111, 60, 17);
            putUnit.setForeground(Color.black);
        }
        return putUnit;
    }

    public void setPutUnit(JLabel putUnit) {
        this.putUnit = putUnit;
    }

    public String[] getUnitString() {
        return unitString;
    }

    public void setUnitString(String[] unitString) {
        this.unitString = unitString;
    }

    public String[] getFundString() {
        if (fundString == null) {
            fundString = new String[3];
            for (int i = 0; i < fundString.length; i++) {
                fundString[i] = "0";
            }
        }
        return fundString;
    }

    public void setFundString(String[] fundString) {
        this.fundString = fundString;
    }

    public LimitNumTextArea getSendBelTextArea() {
        if (sendBelTextArea == null) {
            sendBelTextArea = new LimitNumTextArea(50);
            sendBelTextArea.setLineWrap(true);
            sendBelTextArea.setOpaque(false);
            sendBelTextArea.setForeground(Color.white);
            sendBelTextArea.setBounds(78, 204, 321, 46);
            sendBelTextArea.setCaretColor(Color.white);
        }
        return sendBelTextArea;
    }

    public void setSendBelTextArea(LimitNumTextArea sendBelTextArea) {
        this.sendBelTextArea = sendBelTextArea;
    }

    public boolean isPutIntoBool() {
        return putIntoBool;
    }

    public void setPutIntoBool(boolean putIntoBool) {
        this.putIntoBool = putIntoBool;
    }

    public boolean isChallengeBool() {
        return challengeBool;
    }

    public void setChallengeBool(boolean challengeBool) {
        this.challengeBool = challengeBool;
    }

    public boolean isSendBellBool() {
        return sendBellBool;
    }

    public void setSendBellBool(boolean sendBellBool) {
        this.sendBellBool = sendBellBool;
    }

    public int getWinnerType() {
        return winnerType;
    }

    public void setWinnerType(int winnerType) {
        this.winnerType = winnerType;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public JLabel getRemarkLab() {
        if (remarkLab == null) {
            remarkLab = new JLabel();
            remarkLab.setBounds(36, 26, 350, 16);
            remarkLab.setForeground(Color.black);
            remarkLab.setFont(UIUtils.TEXT_HY16);
            this.add(remarkLab);
        }
        return remarkLab;
    }

    public void setRemarkLab(JLabel remarkLab) {
        this.remarkLab = remarkLab;
    }

    public ImageIcon getPkNameImg() {
        return pkNameImg;
    }

    public void setPkNameImg(ImageIcon pkNameImg) {
        this.pkNameImg = pkNameImg;
    }

    // public ImageIcon getChallengeImg() {
    // return challengeImg;
    // }
    //
    // public void setChallengeImg(ImageIcon challengeImg) {
    // this.challengeImg = challengeImg;
    // }

    public PalacePkBean getPalacePkBean() {
        return palacePkBean;
    }

    public void setPalacePkBean(PalacePkBean palacePkBean) {
        this.palacePkBean = palacePkBean;
    }

}
